$(function () {
    $("#tou").load("tou.html");

    orders(1,5);

});

//用户所有及我的订单  调用一个方法
function orders(pageNum,pageSize) {
    var user = getQueryString("user");
    var url;
    if (user=="admin"){
        //所有用户所有订单
        url = "easybuy/alluserorderlist";
    }else {
        url = "easybuy/userorderlist";
    }
    $.ajax({
        url:url,
        data:{
            pageNum:pageNum,
            pageSize:pageSize
        },
        success:function (pageInfo) {
            var orders=pageInfo.list;
            if (orders.length!=0){
                $(".order_tab").empty();
                $(".mem_tit span").html("我的订单");
                $(".order_tab").eq(0).append("" +
                    "                <tr>\n" +
                    "                    <td width=\"20%\">订单号</td>\n" +
                    "                    <td width=\"25%\">下单时间</td>\n" +
                    "                    <td width=\"15%\">订单总金额</td>\n" +
                    "                    <td width=\"20%\">订单状态</td>\n" +
                    "                    <td width=\"20%\">操作</td>\n" +
                    "                </tr>");
                for (var index=orders.length-1;index>=0;index--){
                    var order = orders[index];


                    $(".order_tab").eq(0).append("" +
                        "                <tr data-id="+order.id+">\n" +
                        "                    <td><a href='javascript:;' class='serialNumber' color=\"#ff4e00\">"+order.serialnumber+"</a></td>\n" +
                        "                    <td>"+order.createtime+"</td>\n" +
                        "                    <td>￥"+order.cost+"</td>\n");
                    var nowDate = new Date().getTime();
                    var createDate = new Date(order.createtime).getTime();
                    var time = Number(nowDate)-Number(createDate);
                    if (order.isdelete ==1){
                        $(".order_tab").find("tr").eq(orders.length-index).append(
                            "                    <td>未发货</td>\n" +
                            "                    <td>已取消 &nbsp;&nbsp;&nbsp; <a class='delete'>删除</a></td>\n" +
                            "                </tr>");
                    }else if (time>30*60*1000){
                        $(".order_tab").find("tr").eq(orders.length-index).append("" +
                            "                    <td>已发货</td>\n" +
                            "                    <td><font color=\"#ff4e00\">已确认 &nbsp;&nbsp;&nbsp; </font><a class='delete'>删除</a></td>\n"+
                            "                </tr>"
                        );
                    }else {
                        $(".order_tab").find("tr").eq(orders.length-index).append(
                            "                    <td>未发货</td>\n" +
                            "                    <td><a class='cancel'>取消订单</a></td>\n" +
                            "                </tr>");
                    }
                }
            }else {
                $(".order_tab").empty();
                $(".order_tab").append("<h1 style='line-height: 100px'>暂无订单，快去下单吧！！</h1>");
            }
            //取消订单
            cancelOrder();
            //删除订单
            deleteOrder();

            //加载订单详情
            orderDetails();

            //分页
            if (orders.length!=0){
                pages(pageInfo,pageNum,pageSize);
            }
        }
    })
}
//取消订单
function cancelOrder() {
    $(".cancel").click(function () {
        var orderId = $(this).parent().parent().data("id");
        $(this).parent().html("已取消  <a class='delete'>删除订单</a>");
        $.ajax({
            url: "easybuy/cancelorder",
            data:{
                orderId:orderId
            },
            success:function (result) {
                if (result){
                    alert("取消成功！");
                }
            }
        });
        deleteOrder();
    });
}

//删除订单
function deleteOrder() {
    $(".delete").click(function () {
        var orderId = $(this).parent().parent().data("id");
        $(this).parent().parent().attr("id","delOrder");
        $.ajax({
            url: "easybuy/deleteorder",
            data:{
                orderId:orderId
            },
            success:function (result) {
                if (result){
                    $("#delOrder").remove();
                    alert("删除成功！");
                    location.href="adminOrder.html";
                }
            }
        });
    });
}

//加载订单详情
function orderDetails() {
    $(".serialNumber").click(function () {
        var $tr = $(this).parent().parent();
        var orderId = $tr.data("id");

        location.href = "adminOrderDetail.html?orderId="+orderId;
    });
}

//取得地址栏参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg); //r是一个数组
    if (r != null) return unescape(r[2]); return null;}

//订单分页
function pages(pageInfo,pageNum,pageSize) {
    $(".pages").empty();


    $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='first'>首页</a>");
    $("#first").click(function () {
        orders(1,pageSize);
    });

    if (pageNum!=1){
        //上一页
        $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='pre'>上一页</a>");
    }
    $("#pre").click(function () {
        orders(pageNum-1,pageSize);
    });

    //页数
    for (let i = 0; i < pageInfo.navigatepageNums.length; i++) {
        if (pageNum == i+1) {
            $(".pages").append("<a href=\"javascript:;\" class=\"cur number\">" + (i + 1) + "</a>");
        } else {
            $(".pages").append("<a href=\"javascript:;\" class='number'>" + (i + 1) + "</a>");
        }
    }
    $(".number").click(function () {
        var pageNum=$(this).text();
        orders(pageNum,pageSize);
    })

    if (pageNum!=pageInfo.navigatepageNums.length){
        //下一页
        $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='next'>下一页</a>");
    }


    $("#next").click(function () {
        orders(pageNum+1,pageSize)
    })
    $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='last'>末页</a>");
    $("#last").click(function () {
        orders(pageInfo.navigatepageNums.length,pageSize)
    })
}

//订单详情加载
function allUserOrderDetails(pageNum,pageSize) {

    var user = getQueryString("user");
    var url;
    if (user=="admin"){
        //所有用户所有订单
        url = "easybuy/alluserorderlist";
    }else {
        url = "easybuy/userorderlist";
    }

    $.ajax({
        url:url,
        data:{
            pageNum:pageNum,
            pageSize:pageSize
        },
        success:function (pageInfo) {
            var orders = pageInfo.list;
            if (orders.length!=0){
                $(".mem_tit").html("订单详情<a href=\"\"class=\"fr\">返回</a>");
                $(".order_tab").empty();

                $.each(orders, function (index) {
                    var order =  orders[index];
                    $(".order_tab").eq(0).append("" +
                        "<tr class=\"td_bg\">\n"+
                        "    <td>用户名:"+order.loginname+"</td>\n"+
                        "    <td><a href=\"javascript:;\">订单号:"+order.serialnumber+"</a></td>\n" +
                        "    <td>地址:"+order.useraddress+"</td>\n"+
                        "    <td>￥"+order.cost+"</td>\n"+
                        "</tr>\n"+
                        "<tr>\n"+
                        "    <td colspan=\"4\">\n" +
                        "    <table border=\"0\" class=\"order_tab\" style=\"width:930px; text-align:center; margin-bottom:30px;\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                        "      <tbody class = \"detail\">\n" +
                        "          <tr>\n"+
                        "             <td width=\"20%\">商品名称</td>\n"+
                        "             <td width=\"20%\">商品图片</td>\n"+
                        "             <td width=\"25%\">数量</td>\n"+
                        "             <td width=\"25%\">价格</td>\n"+
                        "             </tr>\n"+
                        "      </tbody>\n"+
                        "    </table>\n"+
                        "    </td>\n"+
                        "</tr>");

                    var orderId = order.id;
                    $.ajax({
                        url:"easybuy/orderdetails",
                        data:{
                            orderId:orderId
                        },
                        success:function (data) {
                            var orderDetails = data[0];
                            var products = data[1];
                            var order = data[2];
                            var user = data[3];

                            $.each(orderDetails,function (index1,orderDetail) {
                                var product = products[index1];
                                $(".detail").eq(index).append(
                                    '<tr>\n' +
                                    '    <td>'+product.name+'</td>\n' +
                                    '    <td>\n' +
                                    '        <a href="javascript:;" target="_blank">\n' +
                                    '        <img src="http://47.104.137.246:9999/files/'+product.filename+'" width="50" height="50">\n' +
                                    '        </a>\n' +
                                    '    </td>\n' +
                                    '    <td>'+orderDetail.quantity+'</td>\n' +
                                    '    <td>￥'+orderDetail.cost+'</td>\n' +
                                    '</tr>');
                            })
                        }
                    });
                })
                detailsPages(pageInfo,pageNum,pageSize);


            }else {
                $(".order_tab").empty();
                $(".order_tab").append("<h1 style='line-height: 100px'>暂无订单，快去下单吧！！</h1>");
            }

            //分页
            // if (orders.length!=0){
            //     detailsPages(pageInfo,pageNum,pageSize);
            // }
        }
    })
}

//订单详情分页
function detailsPages(pageInfo,pageNum,pageSize) {
    $(".pages").empty();


    $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='first'>首页</a>");
    $("#first").click(function () {
        allUserOrderDetails(1,pageSize);
    });

    if (pageNum!=1){
        //上一页
        $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='pre'>上一页</a>");
    }
    $("#pre").click(function () {
        allUserOrderDetails(pageNum-1,pageSize);
    });

    //页数
    for (let i = 0; i < pageInfo.navigatepageNums.length; i++) {
        if (pageNum == i+1) {
            $(".pages").append("<a href=\"javascript:;\" class=\"cur number\">" + (i + 1) + "</a>");
        } else {
            $(".pages").append("<a href=\"javascript:;\" class='number'>" + (i + 1) + "</a>");
        }
    }
    $(".number").click(function () {
        var pageNum=$(this).text();
        allUserOrderDetails(pageNum,pageSize);
    })

    if (pageNum!=pageInfo.navigatepageNums.length){
        //下一页
        $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='next'>下一页</a>");
    }


    $("#next").click(function () {
        allUserOrderDetails(pageNum+1,pageSize)
    })
    $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='last'>末页</a>");
    $("#last").click(function () {
        allUserOrderDetails(pageInfo.navigatepageNums.length,pageSize)
    })
}
